/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.TaiKhoan_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class TaiKhoan_DAO {
    
    public static String Get_MaTK() throws SQLException{
        String MaTK = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "TK");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaTK = Rs.getString(1);
        }
        return MaTK;
    }
    
    public static ArrayList<TaiKhoan_DTO> GetALL()
    {
        ArrayList<TaiKhoan_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from TaiKhoan" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                TaiKhoan_DTO TaiKhoan = new TaiKhoan_DTO();
                TaiKhoan.setMaTK(rs.getString("MaTK"));
                TaiKhoan.setTenTK(rs.getString("TenTK"));
                TaiKhoan.setMatKhau(rs.getString("MatKhau"));
                TaiKhoan.setActive(rs.getBoolean("Active"));
                TaiKhoan.setMaVaiTro(rs.getString("MaVaiTro"));
                 TaiKhoan.setMaNV(rs.getString("MaNV"));
                ds.add(TaiKhoan);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(TaiKhoan_DTO TaiKhoan)
    {
        boolean flag = false;
        try {
            String sql = "Insert TaiKhoan Values('"+TaiKhoan.getMaTK()+"','"+TaiKhoan.getTenTK()+"','"+TaiKhoan.getMatKhau()+"','"+TaiKhoan.isActive()+"','"+TaiKhoan.getMaVaiTro()+"','"+TaiKhoan.getMaNV()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(TaiKhoan_DTO TaiKhoan)
    {
        boolean flag = false;
        try {
            String sql = "Update TaiKhoan Set TenTK = '"+ TaiKhoan.getTenTK() +"', MatKhau = '"+TaiKhoan.getMatKhau()+"', Active = "+TaiKhoan.isActive()+", MaVaiTro = '"+ TaiKhoan.getMaVaiTro()+ "', MaNV = '"+TaiKhoan.getMaNV()+"' where MaTK = '"+ TaiKhoan.getMaTK()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maTK)
    {
        boolean flag = false;
        try {
            String sql = "Delete from TaiKhoan where MaTK = '"+maTK+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
